package com.fs.web.base.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fs.web.base.mapper.ModleMapper;
import com.fs.web.base.param.SysConstant;
import com.fs.common.pojo.Modle;
import com.fs.web.base.utils.ModleTools;
import com.fs.web.base.vo.JsonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ACE
 * @desc 模块数据controller
 * @creationTime 2019-04-11 21:47
 * @projectName weixinframe
 * @packageName com.fs.web.base.controller
 */
@Slf4j
@Controller
@RequestMapping("/console/modle/rewrite")
public class ModleController {
    @Autowired
    private ModleMapper modleMapper;

    @ResponseBody
    @RequestMapping("/init")
    public JsonResponse init(){
        try {
            String[] classNames = ModleTools.modleClassList("com/fs/common/pojo/*.class");
            Integer count = 0;
            List<Modle> list = new ArrayList<Modle>();
            for(String className: classNames){
                QueryWrapper<Modle> wrapper = new QueryWrapper<>();
                wrapper.eq("class_name",className);
                count = modleMapper.selectCount(wrapper);
                if(count==0){
                    Modle _modle = Modle.newInstance(className);
                    list.add(_modle);
                    /*
                     * 动态添加实体模块名称同类名的对应关系
                     */
                    SysConstant.MODLE_CLASSNAME.put(_modle.getModleName(), _modle.getClassName());
                }
            }
            for (Modle modle : list) {
                modleMapper.insert(modle);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return JsonResponse.error(e.getMessage());
        }
        return JsonResponse.success("初始化成功");
    }
}
